<template>
  <div>
    <div class="header">
      <div class="site-topbar">
        <div class="container">
          <ul class="site-topbar-left">
            <li>
              <a href>小米商城</a>
            </li>
            <li>
              <a href>MIUI</a>
            </li>
            <li>
              <a href>loT</a>
            </li>
            <li>
              <a href>云服务</a>
            </li>
            <li>
              <a href>金融</a>
            </li>
            <li>
              <a href>有品</a>
            </li>
            <li>
              <a href>有品</a>
            </li>
            <li>
              <a href>有品</a>
            </li>
            <li>
              <a href>小爱开放平台</a>
            </li>
            <li>
              <a href>企业团购</a>
            </li>
            <li>
              <a href>资质证照</a>
            </li>
            <li>
              <a href>协议规则</a>
            </li>
            <li>
              <a href>下载app</a>
            </li>
            <li>
              <router-link to="classify">商品分类</router-link>
            </li>
          </ul>
          <ul class="site-topbar-right">
            <li>
              <router-link v-show="!uname" to="/login">登录</router-link>
              <a class="user" v-show="uname">
                欢迎:
                {{uname}}
              </a>
            </li>
            <li v-show="!uname">
              <router-link to="/register">注册</router-link>
            </li>
            <li v-show="uname">
              <a href="javascript:;" @click="logout()">退出</a>
            </li>
            <li>
              <span class="iconfont icongouwuche"></span>
              <a href="javascript:;" @click="goCart">购物车({{list.length}})</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <!-- 搜索二级菜单 -->
    <div class="search">
      <div class="logo">
        <!-- <img src="../assets/logo.jpg" alt /> -->
      </div>
      <ul>
        <li>
          <a href>小米手机</a>
          <ul>
            <li>
              <a href>
                <img src="../assets/001.jpg" />
              </a>
              <p>
                <a href>小米cc9Pro</a>
              </p>
              <p>
                <a href>2599元起</a>
              </p>
            </li>
            <li>
              <a href>
                <img src="../assets/002.jpg" />
              </a>
              <p>
                <a href>小米cc9Pro</a>
              </p>
              <p>
                <a href>2599元起</a>
              </p>
            </li>
            <li>
              <a href>
                <img src="../assets/003.jpg" />
              </a>
              <p>
                <a href>小米cc9Pro</a>
              </p>
              <p>
                <a href>2599元起</a>
              </p>
            </li>
            <li>
              <a href>
                <img src="../assets/004.jpg" />
              </a>
              <p>
                <a href>小米cc9Pro</a>
              </p>
              <p>
                <a href>2599元起</a>
              </p>
            </li>
            <li>
              <a href>
                <img src="../assets/005.jpg" />
              </a>
              <p>
                <a href>小米cc9Pro</a>
              </p>
              <p>
                <a href>2599元起</a>
              </p>
            </li>
            <li>
              <a href>
                <img src="../assets/006.jpg" />
              </a>
              <p>
                <a href>小米cc9Pro</a>
              </p>
              <p>
                <a href>2599元起</a>
              </p>
            </li>
          </ul>
        </li>
        <li>
          <a href>Redmi红米</a>
        </li>
        <li>
          <a href>电视</a>
        </li>
        <li>
          <a href>笔记本</a>
        </li>
        <li>
          <a href>家电</a>
        </li>
        <li>
          <a href>路由器</a>
        </li>
        <li>
          <a href>智能硬件</a>
        </li>
        <li>
          <a href>服务</a>
        </li>
        <li>
          <a href>社区</a>
        </li>
      </ul>
      <div>
        <input type="text" />
        <span class="iconfont iconsousuo"></span>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      uname: sessionStorage.getItem("uname")
    };
  },
  computed: {
    ...mapState({ list: state => state.cart.list })
  },
  created() {
    this.$store.dispatch("getCart");
  },
  watch: {
    $route(to, from) {
      // 对路由变化作出响应...
      console.log(to);
      console.log(from);
      // 只要有路由变化, 就更新登录状态的用户名
      this.uname = sessionStorage.getItem("uname");
      console.log("uname:", this.uname);
    }
  },
  methods: {
    logout() {
       this.$store.commit("setSessionUser", '');
      sessionStorage.clear(); //清空session记录
      this.uname = null;
      this.$store.state.cart.list=[]
    },
    goCart(){
      let uid = sessionStorage.getItem("uid");
      console.log(uid);
      if(!uid){
        alert("请先登录")
        this.$router.push('/login')
        return
      }
      this.$router.push({name:'Cart',query: {uid:uid}})
    }
  }
};
</script>
<style scoped>
* {
  margin: 0;
  padding: 0;
}
li {
  list-style: none;
}
a {
  text-decoration: none;
}
body {
  background-color: #f5f5f5;
}
.header {
  width: 100%;
  height: 40px;
  background: #333;
}
.header > .site-topbar {
  width: 1200px;
  height: 100px;
  margin: 0 auto;
}
.header > .site-topbar > .container {
  width: 1200px;
  height: 40px;
}
/*左边*/
.header > .site-topbar > .container > .site-topbar-left {
  width: 75%;
  height: 40px;
  float: left;
}
.header > .site-topbar > .container > .site-topbar-left > li,
.header > .site-topbar > .container > .site-topbar-right > li {
  float: left;
}
.header > .site-topbar > .container > .site-topbar-left > li > a,
.header > .site-topbar > .container > .site-topbar-right > li > a {
  display: inline-block;
  font-size: 12px;
  color: #b0b0b0;
  height: 40px;
  line-height: 40px;
  text-align: center;
  padding-right: 5px;
}
.header > .site-topbar > .container > .site-topbar-left > li + li {
  margin-left: 5px;
}
.header > .site-topbar > .container > .site-topbar-left > li + li:before,
.header > .site-topbar > .container > .site-topbar-right > li + li:before {
  content: "| ";
  opacity: 0.2;
  color: #b0b0b0;
  display: inline;
  margin-right: 5px;
}
/*右边*/
.header > .site-topbar > .container > .site-topbar-right {
  float: right;
  height: 40px;
}
.header
  > .site-topbar
  > .container
  > .site-topbar-right
  > li:last-child
  > span {
  color: gray;
  font-size: 30px;
}
/* 搜索 */
.search {
  margin: 0 auto;
  width: 1400px;
  height: 100px;
  display: flex;
  justify-content: start;
  align-items: center;
  position: relative;
}
.search > ul {
  display: flex;
  justify-content: space-around;
}
.search > ul > li {
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.search > ul > li > a {
  color: #333;
}
.search > ul > li > a:hover {
  color: #ff6700;
}
.search input {
  width: 260px;
  height: 40px;
  border: 1px solid #e0e0e0;
  outline: 0;
  font-size: 16px;
  padding-left: 10px;
}
.search > div span {
  position: relative;
  top: 9px;
  height: 33px;
  left: -39px;
  font-size: 38px;
}
.search > ul > li > ul {
  width: 1400px;
  height: 0;
  position: absolute;
  opacity: 0;
  top: 100px;
  left: 0;
  border-top: 1px solid #fff;
  z-index: 200;
  background-color: #fff;
  overflow: hidden;
  transition: all 0.2s linear;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.search > ul > li > ul > li > a {
  margin-top: 20px;
  display: block;
  height: 102px;
}
.search > ul > li > ul > li > p {
  line-height: 16px;
}
.search > ul > li > ul > li > p:nth-child(2) {
  margin-top: 10px;
}
.search > ul > li > ul > li > p:nth-child(2) > a {
  font-size: 14px;
  color: #333;
}
.search > ul > li > ul > li > p:nth-child(3) > a {
  font-size: 14px;
  color: #ff6700;
}
.search > ul > li:hover {
  cursor: pointer;
}
.search > ul > li:hover > ul {
  top: 100px;
  left: 0;
  height: 200px;
  position: absolute;
  border-top: 1px solid #e0e0e0;
  opacity: 1;
}
.logo {
  width: 55px;
  height: 55px;
  background-image: url("../assets/logo.jpg");
}
.logo:hover {
  background-image: url("../assets/logo-hover.png");
}
.user {
  display: inline-block;
  font-size: 12px;
  color: #b0b0b0;
  height: 40px;
  line-height: 40px;
  text-align: center;
  padding-right: 5px;
}
</style>